import Vue from 'vue'
import VueRouter from 'vue-router'
// import { VueRouter } from 'vue-router/types/router'
// import HomeView from '../views/HomeView.vue'
import Find from '@/views/Find'
import Recommend from '@/components/Recommend'
import Sort from '@/components/Sort'
import ProDetail from '@/views/ProDetail'
import Collect from '@/views/Collect'
import Order from '@/views/Order'
import My from '@/views/My'
import Upload from '@/views/Upload'
import Customer from '@/views/Customer'
import NotFound from '@/views/NotFound'
import store from '@/store'
import { Notify, Loading, Toast } from 'vant'
Vue.use(Notify)
Vue.use(Loading)
Vue.use(Toast)

Vue.use(VueRouter)

const routes = [
  // 访问默认路径直接重定向到首页
  { path: '/', redirect: '/find/recommend' },
  {
    path: '/find',
    component: Find,
    redirect: '/find/recommend',
    children: [
      {
        path: '/find/recommend', component: Recommend
      },
      {
        path: '/find/sort', component: Sort
      }
    ]
  },
  { path: '/prodetail/:id', component: ProDetail },
  { path: '/collect', component: Collect },
  { path: '/order', component: Order },
  { path: '/my', component: My },
  { path: '/upload', component: Upload },
  { path: '/cs', component: Customer },
  { path: '*', component: NotFound }
]

const router = new VueRouter({
  routes
})
// 全局前置导航守卫
// to表示到哪里去
// from表示从哪里来
// next（）表示是否放行
// next(路径)表示拦截到对应页面
const interceptURL = ['/collect', '/order', '/my', '/upload']
router.beforeEach((to, from, next) => {
  // console.log(to, from, next)
  if (!interceptURL.includes(to.path)) {
    next()
    return
  }
  if (store.state.nowUser.username !== '') {
    next()
  } else {
    next('/')
    Toast.loading({
      message: '加载中...',
      forbidClick: true,
      position: top
    })
    setTimeout(() => {
      // document.querySelector('.van-loading').style.display = 'none'
      Notify({
        message: '请先登录',
        color: '#ffffff',
        background: '#ee0a24'
      })
    }, 2000)
  }
})

export default router
